<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form :ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="username" v-if="operType=='add'" >
              <el-tooltip class="item" effect="dark" content="创建后不可修改" placement="top-start">
                <el-input v-model="form.username" placeholder="请输入登录账号" />
              </el-tooltip>
            </el-form-item>
            <el-form-item label="登录账号" v-else>
              <span>{{form.username}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编码" prop="userCode">
              <el-input v-model="form.userCode" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名" prop="realname">
              <el-input v-model="form.realname" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="user_status" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="绑定手机" prop="phone">
              <el-input v-model="form.phone" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <lam-dict-select v-model="form.sex" dictCode="sex" placeholder="" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日" prop="birthDate">
              <el-input v-model="form.birthDate" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户积分" prop="score">
              <el-input-number v-model="form.score" clearable controls-position="right" :min="0" style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否新用户" prop="isNew">
              <lam-dict-select type="radio" v-model="form.isNew" dictCode="yn10" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import { EditMixin } from '@/components/mixins/EditMixin';

export default {
  name: "EditUser",
  mixins:[EditMixin],
  data() {
    return {
      url: {
        base: '/mall/user/'
      },
      // 表单校验
      rules: {
        username: [{required: true, message: "登录账号不能为空", trigger: "blur" }, {validator: this.usernameUniqueValidator, trigger: 'blur'}],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        userCode: null,
        nickName: null,
        realname: null,
        username: null,
        loginPassword: null,
        payPassword: null,
        salt: null,
        status: 0,
        phone: null,
        avatar: null,
        sex: 0,
        email: null,
        birthDate: null,
        score: null,
        isNew: 0,
        regType: 0,
        regIp: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
    },

    usernameUniqueValidator(rule, value, callback) {
      let param = {username: value};
      if(this.operType=='update') param.id = this.form.id;
      this.checkUniqueAsyn(value, callback, 'username', param);
    },

    /** 提交按钮 */
    submitForm(){
      this.saveForm(this.url.base);
    }

  }
};
</script>
